package com.cy1640.damai;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cy1640.damai.adapter.MyFragmentPagerAdapter;
import com.cy1640.damai.yanchu.Yanchu_fg10_children;
import com.cy1640.damai.yanchu.Yanchu_fg11_cartoon;
import com.cy1640.damai.yanchu.Yanchu_fg1_all;
import com.cy1640.damai.yanchu.Yanchu_fg2_concert;
import com.cy1640.damai.yanchu.Yanchu_fg3_music;
import com.cy1640.damai.yanchu.Yanchu_fg4_opera;
import com.cy1640.damai.yanchu.Yanchu_fg5_dance;
import com.cy1640.damai.yanchu.Yanchu_fg6_quyuan;
import com.cy1640.damai.yanchu.Yanchu_fg7_sports;
import com.cy1640.damai.yanchu.Yanchu_fg8_holiday;
import com.cy1640.damai.yanchu.Yanchu_fg9_goods;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyang on 2016/12/12.
 */

public class Yanchufragment extends Fragment {
    private TabLayout mTabLayout;
    private String[] titles={"全部分类","演唱会","音乐会","话剧歌剧","舞蹈芭蕾","曲苑杂坛","体育比赛","度假休闲","周边商品","儿童亲子","动漫"};
    private ViewPager vp_yanchu;
    private List<Fragment> fragments;
    private MyFragmentPagerAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.yanchu_layout,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tv_yanchu= (TextView) view.findViewById(R.id.tv_yanchu);
        tv_yanchu.setText("演出");
        vp_yanchu= (ViewPager) view.findViewById(R.id.vp_yanchu);
        mTabLayout= (TabLayout) view.findViewById(R.id.tablayout);

        initData(); //填充viewpager的数据

        //根据需要设置显示的标题添加到tab中
        for (int i = 0; i <titles.length ; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }

        //绑定viewpager的监听事件 参数传递的TabLayout提供的监听事件 viewpager中的page页与tab页对应
        vp_yanchu.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        //tablayout中的tab页面与ViewPager中page页对应
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp_yanchu.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void initData(){
        fragments=new ArrayList<>();
        Bundle bundle=new Bundle();
        Yanchu_fg1_all yanchu_fg1_all=new Yanchu_fg1_all();
        bundle.putInt("index",1);
        yanchu_fg1_all.setArguments(bundle);

        Yanchu_fg2_concert yanchu_fg2_concert=new Yanchu_fg2_concert();
        bundle=new Bundle();
        bundle.putInt("index",2);
        yanchu_fg2_concert.setArguments(bundle);

        Yanchu_fg3_music yanchu_fg3_music=new Yanchu_fg3_music();
        bundle=new Bundle();
        bundle.putInt("index",3);
        yanchu_fg3_music.setArguments(bundle);

        Yanchu_fg4_opera yanchu_fg4_opera=new Yanchu_fg4_opera();
        bundle=new Bundle();
        bundle.putInt("index",4);
        yanchu_fg4_opera.setArguments(bundle);

        Yanchu_fg5_dance yanchu_fg5_dance=new Yanchu_fg5_dance();
        bundle=new Bundle();
        bundle.putInt("index",5);
        yanchu_fg5_dance.setArguments(bundle);

        Yanchu_fg6_quyuan yanchu_fg6_quyuan=new Yanchu_fg6_quyuan();
        bundle=new Bundle();
        bundle.putInt("index",6);
        yanchu_fg6_quyuan.setArguments(bundle);

        Yanchu_fg7_sports yanchu_fg7_sports=new Yanchu_fg7_sports();
        bundle=new Bundle();
        bundle.putInt("index",7);
        yanchu_fg7_sports.setArguments(bundle);

        Yanchu_fg8_holiday yanchu_fg8_holiday=new Yanchu_fg8_holiday();
        bundle=new Bundle();
        bundle.putInt("index",8);
        yanchu_fg8_holiday.setArguments(bundle);

        Yanchu_fg9_goods yanchu_fg9_goods=new Yanchu_fg9_goods();
        bundle=new Bundle();
        bundle.putInt("index",9);
        yanchu_fg9_goods.setArguments(bundle);

        Yanchu_fg10_children yanchu_fg10_children=new Yanchu_fg10_children();
        bundle=new Bundle();
        bundle.putInt("index",10);
        yanchu_fg10_children.setArguments(bundle);

        Yanchu_fg11_cartoon yanchu_fg11_cartoon=new Yanchu_fg11_cartoon();
        bundle=new Bundle();
        bundle.putInt("index",11);
        yanchu_fg11_cartoon.setArguments(bundle);

        fragments.add(yanchu_fg1_all);
        fragments.add(yanchu_fg2_concert);
        fragments.add(yanchu_fg3_music);
        fragments.add(yanchu_fg4_opera);
        fragments.add(yanchu_fg5_dance);
        fragments.add(yanchu_fg6_quyuan);
        fragments.add(yanchu_fg7_sports);
        fragments.add(yanchu_fg8_holiday);
        fragments.add(yanchu_fg9_goods);
        fragments.add(yanchu_fg10_children);
        fragments.add(yanchu_fg11_cartoon);

        adapter=new MyFragmentPagerAdapter(getFragmentManager(),fragments);
        vp_yanchu.setAdapter(adapter);

    }
}
